package com.vm.action.system;

import java.sql.Timestamp;

import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.common.Var;
import com.vm.dao.system.IAgency;
import com.vm.entity.Agency;
import com.vm.entity.User;
import com.vm.log4j.CustomizeLog4j;

public class AgencyAction extends ActionSuper implements Preparable{

	private static Logger _log = CustomizeLog4j.getLogger(MenuAction.class);
	private Agency agency;
	private IAgency agencyDAO;
	private List<Agency> listTagency;
	
	private String suprOrgNo;
	
	
	public void prepare() throws Exception {
		super.setButtonGrpMap(agencyDAO.seletButtonGrp((User)this.getSession().get("user"), "agencyAction"));
	}
	
	public String add() {
		listTagency = agencyDAO.findByParentid(suprOrgNo,this.getAreaNo(agencyDAO));
		return "add";
	}
	
	public String save(){
		Timestamp time = new Timestamp(System.currentTimeMillis());
		String[] msgArg = new String[] { "agencyAction_add.do?suprOrgNo=" + this.agency.getSuprOrgNo()};
		List<Agency> agencyList = this.agencyDAO.findAll();
		String msg = null;
		try {
			if(agency.getOrgNo()!= null && !"".equals(agency.getOrgNo())&&agencyList!=null){
				for(Agency gc : agencyList){
					if(gc.getOrgNo().equals(agency.getOrgNo())){
						msg = this.getResourceMessage("unique_number", msgArg);
						super.setShowMessage(msg);
						return super.SHOW_MESSAGE_RESULT;
					}
				}
				agency.setRnwTm(time);
				agency.setArGrp(this.agencyDAO.getGrp("Agency", "arGrp"));
				agency.setVldMrk(Var.VALID);
				agency.setArNo(((User)this.getSession().get("user")).getBlngOrgNo());
				agencyDAO.save(agency);
				msg = super.getResourceMessage("insert_agency_success", msgArg);
				}
		} catch (Exception e) {
			_log.error("", e);
			e.printStackTrace();
			msg = super.getResourceMessage("insert_agency_err", msgArg);
		}
		listTagency = agencyDAO.findByParentid(suprOrgNo,this.getAreaNo(agencyDAO));
		agency = new Agency();
		agency.setSuprOrgNo(suprOrgNo);
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}
	public String delete() {
		agency = this.agencyDAO.findById(this.agency.getOrgNo());
		String[] msgArg = new String[] { "agencyAction_add.do?suprOrgNo=" + this.agency.getSuprOrgNo() };
		String msg = null;
		try {
			agencyDAO.deleteById(agency.getOrgNo());
			msg = super.getResourceMessage("delete_agency_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("delete_agency_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}
	
	public String list() {
		listTagency = agencyDAO.findByParentid(suprOrgNo,this.getAreaNo(agencyDAO));
		return SUCCESS;
	}
	
	public String view() {
		agency = agencyDAO.findById(suprOrgNo);
		return SUCCESS;
	}
	
	public String edit() {
		agency = agencyDAO.findById(this.agency.getOrgNo());
		listTagency = agencyDAO.findByParentid(agency.getSuprOrgNo(),this.getAreaNo(agencyDAO));
		return SUCCESS;
	}
	
	public String update() {
		Timestamp time = new Timestamp(System.currentTimeMillis());
		String[] msgArg = new String[] { "agencyAction_add.do?suprOrgNo=" + this.agency.getSuprOrgNo()};
		String msg = null;
		try {
			agency.setRnwTm(time);
			agencyDAO.update(agency);
			msg = super.getResourceMessage("update_agency_success", msgArg);
		} catch (Exception e) {
			_log.error("", e);
			msg = super.getResourceMessage("update_agency_err", msgArg);
		}
		listTagency = agencyDAO.findByParentid(suprOrgNo,this.getAreaNo(agencyDAO));
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}
	

	public Agency getAgency() {
		return agency;
	}
	
	public void setAgency(Agency agency) {
		this.agency = agency;
	}
	
	public IAgency getAgencyDAO() {
		return agencyDAO;
	}
	
	public void setAgencyDAO(IAgency agencyDAO) {
		this.agencyDAO = agencyDAO;
	}
	
	public List<Agency> getListTagency() {
		return listTagency;
	}
	
	public void setListTagency(List<Agency> listTagency) {
		this.listTagency = listTagency;
	}

	public String getSuprOrgNo() {
		return suprOrgNo;
	}

	public void setSuprOrgNo(String suprOrgNo) {
		this.suprOrgNo = suprOrgNo;
	}
	
}
